﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BuscoPre.Domain;
using BuscoPre.Repository.MLA;
using BuscoPre.Repository.MLA.JsonTypes;

namespace BuscoPre.Repository
{
	public class ArticleDao
	{
		private static readonly string HotItemRequestUrl = "https://api.mercadolibre.com/sites/MLA/hot_items/search?limit=";
		private static readonly string SearchItemRequestUrl = "https://api.mercadolibre.com/sites/MLA/search?limit={0}&q={1}";
		private static readonly string SearchItemByCategoryIdRequestUrl = "https://api.mercadolibre.com/sites/MLA/search?category={0}&limit={1}";

		public static List<Article> GetArticlesByCategory(int limit, string categoryId)
		{
			try
			{
				var q = string.Format(SearchItemByCategoryIdRequestUrl, categoryId, limit);

				GenericRestMessageService<Article> service = new SearchItemRestMessageService();
				var response = service.Get(q);

				if (response != null)
				{
					// La respuesta se obtiene a un SearchResult creado por la herramienta.
					var results = new SearchResult(response.ToString());

					foreach (var filter in results.Filters)
					{
						foreach (var value in filter.Values)
						{
							foreach (var path in value.PathFromRoot)
							{
								//Aca vamos a tener que generar las urls para el menu y para los ID de subcategorias.
								//Ojo hay varios filtros que pueden haber, por ejemplo "menor precio". Revisar.
							}
						}
					}

					if (results != null)
					{
						var articles = new List<Article>();
						foreach (var item in results.Results)
						{
							var article = new Article
							{
								Id = item.Id,
								Title = item.Title,
								Price = item.Price,
								Permalink = item.Permalink,
								Subtitle = item.Subtitle,
								Thumbnail = item.Thumbnail
							};
							articles.Add(article);
						}
						return articles.OrderBy(item => item.Title).ToList();
					}
				}

				return null;
			}
			catch (Exception)
			{

				throw;
			}
		}

		public static List<Article> GetArticlesByQuery(string query, int limit)
		{
			try
			{
				var q = string.Format(SearchItemRequestUrl, limit.ToString(), query);

				GenericRestMessageService<Article> service = new SearchItemRestMessageService();
				var response = service.Get(q);

				if (response != null)
				{
					// La respuesta se obtiene a un SearchResult creado por la herramienta.
					var results = new SearchResult(response.ToString());

					if (results != null)
					{
						var articles = new List<Article>();
						foreach (var item in results.Results)
						{
							var article = new Article
							{
								Id = item.Id,
								Title = item.Title,
								Price = item.Price,
								Permalink = item.Permalink,
								Subtitle = item.Subtitle,
								Thumbnail = item.Thumbnail
							};
							articles.Add(article);
						}
						return articles.OrderBy(item => item.Title).ToList();
					}
				}

				return null;
			}
			catch (Exception)
			{

				throw;
			}
		}


		public static List<Article> GetHotItems(int limit)
		{
			try
			{
				//string fullRequestUrl = string.Concat(HotItemRequestUrl, limit.ToString());
				////var response = RestMessageService.Get(fullRequestUrl);
				//GenericRestMessageService<Article> service = new HotItemsRestMessageService();
				//var response = service.Get(fullRequestUrl);

				//if (response != null)
				//{
				//    var hotItems = new List<Article>();

				//    foreach (var item in response)
				//    {
				//        var hotItem = new Article
				//        {
				//            Title = item["title"].ToString().Replace('"', ' ')
				//        };
				//        hotItems.Add(hotItem);
				//    }
				//    return hotItems;
				//}
				return null;
			}
			catch (Exception)
			{
				throw;
			}
		}
	}
}
